package cn.cew.bytespy.comm;

import cn.cew.bytespy.utils.BaseUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenenwei
 * @version 1.0.0
 * @description TODO
 * @createTime 2022-10-15 13:33:00
 */
public class AgentParam {

    private static final String GLOBAL_VARS = "globalVars";
    private static final String LOCAL_VARS = "localVars";
    public static final String IP = "ip";
    public static final String PORT = "port";
    public static final String TRANSFORMER_TYPE = "type";
    public static final String OPERATOR = "operator";
    public static final String PACKAGE_NAME = "pName";
    public static final String CLASS_NAME = "cName";
    public static final String METHOD_NAME = "mName";
    public static final String RECOVER = "recover";
    public static final String CREATE_PROXY = "proxy";
    public static final String LINE_CODE = "lCode";
    public static final String METHOD_BODY = "mBody";
    public static final String IMPORT_PACKAGES = "packages";
    public static final String OUT_DIR = "outDir";
    public static final String SIGNATURE = "signature";
    /**
     * 服务端ip
     */
    private String ip = "127.0.0.1";
    /**
     * 服务端端口
     */
    private int port = 8888;
    /**
     * 类型
     */
    private String type;
    /**
     * 包名
     */
    private String packageName;
    /**
     * 类名
     */
    private String className;
    /**
     * 方法名
     */
    private String methodName;
    /**
     * 是否恢复
     */
    private boolean recover;
    /**
     * 创建代理类
     */
    private boolean createProxy;
    /**
     * 行内容
     */
    private String lineCode;
    /**
     * 方法体
     */
    private String methodBody;
    /**
     * 需要导入的包
     */
    private List<String> importPackages;
    /**
     * 日志输出文件路径
     */
    private String outDir;
    /**
     * 方法签名
     */
    private String signature;
    /**
     * 全局变量名列表
     */
    private List<String> globalVars;
    /**
     * 局部变量名列表
     */
    private List<String> localVars;

    /**
     * 操作类型
     */
    private List<String> operators;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public boolean isRecover() {
        return recover;
    }

    public void setRecover(boolean recover) {
        this.recover = recover;
    }

    public boolean isCreateProxy() {
        return createProxy;
    }

    public void setCreateProxy(boolean createProxy) {
        this.createProxy = createProxy;
    }

    public String getMethodBody() {
        return methodBody;
    }

    public void setMethodBody(String methodBody) {
        this.methodBody = methodBody;
    }

    public List<String> getImportPackages() {
        return importPackages;
    }

    public void setImportPackages(List<String> importPackages) {
        this.importPackages = importPackages;
    }

    public String getLineCode() {
        return lineCode;
    }

    public void setLineCode(String lineCode) {
        this.lineCode = lineCode;
    }

    public String getOutDir() {
        return outDir;
    }

    public void setOutDir(String outDir) {
        this.outDir = outDir;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public List<String> getGlobalVars() {
        return globalVars;
    }

    public void setGlobalVars(List<String> globalVars) {
        this.globalVars = globalVars;
    }

    public List<String> getLocalVars() {
        return localVars;
    }

    public void setLocalVars(List<String> localVars) {
        this.localVars = localVars;
    }

    public List<String> getOperators() {
        return operators;
    }

    public void setOperators(List<String> operators) {
        this.operators = operators;
    }

    public AgentParam (String args) throws Exception {
        if (BaseUtils.isEmpty(args)) {
            throw new Exception("命令参数不能为空");
        }

        // 格式为： type:1@pName:cn.cew.xxx@packages:java.lang.String,java.lang.Integer
        String[] allParams = args.split("@");
        for (String allParam : allParams) {
            String[] param = allParam.split(":");
            if (param.length < 2) {
                continue;
            }
            String key = param[0];
            String value = param[1];
            if (TRANSFORMER_TYPE.equals(key)) {
                this.type = value;
            } else if (PACKAGE_NAME.equals(key)) {
                this.packageName = value;
            } else if (CLASS_NAME.equals(key)) {
                this.className = value;
            } else if (METHOD_NAME.equals(key)) {
                this.methodName = value;
            } else if (RECOVER.equals(key)) {
                this.recover = Boolean.parseBoolean(value);
            } else if (CREATE_PROXY.equals(key)) {
                this.createProxy = Boolean.parseBoolean(value);
            } else if (LINE_CODE.equals(key)) {
                this.lineCode = value;
            } else if (METHOD_BODY.equals(key)) {
                this.methodBody = value;
            } else if (IMPORT_PACKAGES.equals(key)) {
                this.importPackages = BaseUtils.toList(value, ",");
            } else if (OUT_DIR.equals(key)) {
                this.outDir = value;
            } else if (SIGNATURE.equals(key)) {
                this.signature =value;
            } else if (IP.equals(key)) {
                this.ip = value;
            } else if (PORT.equals(key)) {
                this.port = Integer.parseInt(value);
            } else if (GLOBAL_VARS.equals(key)) {
                this.globalVars = BaseUtils.toList(value, ",");
            } else if (LOCAL_VARS.equals(key)) {
                this.localVars = BaseUtils.toList(value, ",");
            } else if (OPERATOR.equals(key)) {
                this.operators = BaseUtils.toList(value, ",");
            }
        }
        if (BaseUtils.isEmpty(importPackages)) {
            importPackages = new ArrayList<>();
        }
    }


}
